using AdminAuthority.Attributes;

using Abc.Application;
using Abc.Core;
using Abc.Web.Core;

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

using System.Threading.Tasks;

namespace Abc.Web.Mvc.Areas.HtAdmin.Controllers
{
    //[AllowAnonymous]
    [AdminAuthorize]
    //[PermissionFilter()]//登陆授权
    [AuthorityController(typeCode: "HtAdmin", controllerDescription: "后台首页")]
    public class HomeController : HtAdminBaseController
    {
        private readonly ILogger<MenuController> _logger;
        private readonly ICacheService _cacheService;
        private readonly IDataConfigService _dataConfigService;
        private readonly IUserService _userService;
        public HomeController(ILogger<MenuController> logger
        , ICacheService cacheService
        , IDataConfigService dataConfigService
        , IUserService userService)
        {
            _logger = logger;
            _cacheService = cacheService;
            _dataConfigService = dataConfigService;
            _userService = userService;
        }

        [AuthorityPermission(name: "浏览", actions: "Index,GetMenus")]
        public async Task<IActionResult> Index()
        {
            ViewData["SystemSetting"] = await _dataConfigService.GetSetting<SystemSettingDto>("SystemSetting");
            var admindto = await _userService.GetLoginUser();
            return View(admindto);
        }

        /// <summary>
        /// 控制台
        /// </summary>
        /// <returns></returns>
        [AuthorityPermission(name: "控制台", actions: "Console")]
        public IActionResult Console()
        {
            return View();
        }

        public IActionResult Console2()
        {
            return View();
        }

        [HttpGet]
        public async Task<JsonResult> GetMenus()
        {
            var pagedlist = await _userService.GetMenus();
            return Json(pagedlist);
        }

        /// <summary>
        /// 刷新缓存
        /// </summary>
        /// <returns></returns>
        [AuthorityPermission(name: "刷新缓存", actions: "RefreshCache")]
        public async Task<JsonResult> RefreshCache()
        {
            var resultdata = await _cacheService.RefreshCache();
            return Json(resultdata);
        }
    }
}